﻿using UnityEngine;
using System.Collections;

public class BattleUI : MonoBehaviour {

	private static BattleUI instance;
	
	public static BattleUI Instance{
		get{
			return instance;
		}
	}	

	public UIToggle x4Toggle;

	public GameObject effectFontRes_heal;
	public GameObject effectFontRes_damage_player;
	public GameObject effectFontRes_damage_normal_enemy;
	public GameObject effectFontRes_damage_skill_enemy;

	public HUDText[] playerDamageHUDText;
	public HUDText[] enermyDamageHUDText;
	public HUDText[] enemySkillDamageHUDText;
	public HUDText[] playerHealHUDText;

	void Awake()
	{
		instance=this;

		playerDamageHUDText = new HUDText[5];
		enermyDamageHUDText = new HUDText[5];
		enemySkillDamageHUDText = new HUDText[5];
		playerHealHUDText = new HUDText[5];
	}

	void Start()
	{
		effectFontRes_damage_player = BattleManager.Instance.uiRootTrans.FindChild("LabelPlayerDamage").gameObject;
		effectFontRes_damage_normal_enemy = BattleManager.Instance.uiRootTrans.FindChild("LabelEnemyDamage").gameObject;
		effectFontRes_damage_skill_enemy = BattleManager.Instance.uiRootTrans.FindChild("LabelEnemySkillDamage").gameObject;
		effectFontRes_heal = BattleManager.Instance.uiRootTrans.FindChild("LabelHeal").gameObject;
		//		effectFontRes_damage_player = Resources.Load ("Prefab/UI/LabelPlayerDamage") as GameObject;
		//		effectFontRes_damage_normal_enemy = Resources.Load ("Prefab/UI/LabelEnemyDamage") as GameObject;
		//		effectFontRes_damage_skill_enemy = Resources.Load ("Prefab/UI/LabelEnemyDamage") as GameObject;
		//		effectFontRes_heal = Resources.Load ("Prefab/UI/LabelHeal") as GameObject;
	}

	HUDText CreateHudText(int nType)
	{
		if(nType==1) {//playerDamageHUDText
			//if(effectFontRes_damage_player==null)
			//	effectFontRes_damage_player = Resources.Load ("Prefab/UI/LabelPlayerDamage") as GameObject;  //BattleManager.Instance.uiRootTrans.FindChild("LabelPlayerDamage").gameObject;
			GameObject child = NGUITools.AddChild(BattleManager.Instance.uiRootTrans.gameObject, effectFontRes_damage_player);
			child.SetActive(true);
			return child.GetComponent<HUDText>();
		}
		else if(nType==2) {//enermyDamageHUDText
			//if(effectFontRes_damage_normal_enemy==null)
			//	effectFontRes_damage_normal_enemy = Resources.Load ("Prefab/UI/LabelEnemyDamage") as GameObject;//BattleManager.Instance.uiRootTrans.FindChild("LabelEnemyDamage").gameObject;
			GameObject child = NGUITools.AddChild(BattleManager.Instance.uiRootTrans.gameObject, effectFontRes_damage_normal_enemy);
			child.SetActive(true);
			return child.GetComponent<HUDText>();
		}
		else if(nType==3) {//enemySkillDamageHUDText
			//if(effectFontRes_damage_skill_enemy==null)
			//	effectFontRes_damage_skill_enemy = Resources.Load ("Prefab/UI/LabelEnemyDamage") as GameObject;//BattleManager.Instance.uiRootTrans.FindChild("LabelEnemySkillDamage").gameObject;
			GameObject child = NGUITools.AddChild(BattleManager.Instance.uiRootTrans.gameObject, effectFontRes_damage_skill_enemy);
			child.SetActive(true);
			return child.GetComponent<HUDText>();
		}
		else if(nType==4) {//playerHealHUDText
			//if(effectFontRes_heal==null)
			//	effectFontRes_heal = Resources.Load ("Prefab/UI/LabelHeal") as GameObject;//BattleManager.Instance.uiRootTrans.FindChild("LabelHeal").gameObject;
			GameObject child = NGUITools.AddChild(BattleManager.Instance.uiRootTrans.gameObject, effectFontRes_heal);
			child.SetActive(true);
			return child.GetComponent<HUDText>();
		}
		return null;
	}

	public void ShowDamageHUDText(bool isPlayer, int index, int damage, bool isAttackedBySkill)
	{
		if( isPlayer )
		{
			if(playerDamageHUDText[index-1]==null)
				playerDamageHUDText[index-1] = CreateHudText(1);
			UIFollowTarget ftarget = playerDamageHUDText[index-1].GetComponent<UIFollowTarget>();
			if( ftarget == null )
				ftarget = playerDamageHUDText[index-1].gameObject.AddComponent<UIFollowTarget>();
			ftarget.target = BattleManager.Instance.playerList[index-1].pawnManifest.hudHeadTrans;
			if(ftarget.target !=null && ftarget.IsTargetVisible())
				playerDamageHUDText[index-1].Add(damage.ToString(), Color.white, 0f, 2);
		}
		else
		{
			if(isAttackedBySkill) {
				if(enemySkillDamageHUDText[index-1]==null)
					enemySkillDamageHUDText[index-1] = CreateHudText(3);
				UIFollowTarget ftarget = enemySkillDamageHUDText[index-1].GetComponent<UIFollowTarget>();
				if( ftarget == null ) 
					ftarget = enemySkillDamageHUDText[index-1].gameObject.AddComponent<UIFollowTarget>();
				ftarget.target = BattleManager.Instance.enemyList[index-1].pawnManifest.hudHeadTrans;
				if(ftarget.target !=null && ftarget.IsTargetVisible())
					enemySkillDamageHUDText[index-1].Add(damage.ToString(), Color.white, 0f, 2);
			}
			else {
				if(enermyDamageHUDText[index-1]==null)
					enermyDamageHUDText[index-1] = CreateHudText(2);
				UIFollowTarget ftarget = enermyDamageHUDText[index-1].GetComponent<UIFollowTarget>();
				if( ftarget == null ) 
					ftarget = enermyDamageHUDText[index-1].gameObject.AddComponent<UIFollowTarget>();
				ftarget.target = BattleManager.Instance.enemyList[index-1].pawnManifest.hudHeadTrans;
				if(ftarget.target !=null && ftarget.IsTargetVisible())
					enermyDamageHUDText[index-1].Add(damage.ToString(), Color.white, 0f, 2);
			}
		}
	}

	public void ClearDamageHUDText(bool isPlayer, int index)
	{
		if( isPlayer )
		{
			if(playerDamageHUDText[index-1]!=null)
				Destroy( playerDamageHUDText[index-1].gameObject.GetComponent<UIFollowTarget>() );
		}
		else
		{
			if(enermyDamageHUDText[index-1]!=null)
				Destroy( enermyDamageHUDText[index-1].gameObject.GetComponent<UIFollowTarget>() );
		}
	}

	public void OnX4ButtonPress()
	{
		x4Toggle.enabled = true;
		x4Toggle.value = true;
		x4Toggle.enabled = false;
		
		EventlineTimeScale.Multiply = 4;
		
		if (CamTakeSetting.Instance != null)
		{
			CamTakeSetting.Instance.allowTouchCam = false;
			CamTakeSetting.Instance.DeactivateFreeCam();
		}
	}
	
	public void OnX4ButtonRelease()
	{
		x4Toggle.enabled = true;
		x4Toggle.value = false;
		x4Toggle.enabled = false;
		
		EventlineTimeScale.Multiply = 1;
		
		if (CamTakeSetting.Instance != null)
		{
			CamTakeSetting.Instance.allowTouchCam = true;
		}
	}

	public void SetPlayerInfos()
	{
	}

	public void ResetEnermyHpBar()
	{
	}
}
